package com.cupcorn.lib.display;

import org.andengine.entity.scene.Scene;
import org.andengine.extension.tmx.TMXLayer;
import org.andengine.extension.tmx.TMXLoader;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.extension.tmx.util.exception.TMXLoadException;

import com.cupcorn.lib.Utils;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.element.GameElementTmx;

public class DisplayTmx
{
	GameEngine				engine;
	GameElementTmx[]		gameElementTmx;
	public TMXTiledMap[]	tiledMap;
	public TMXLayer[][]		tmxLayer;
	
	public DisplayTmx(final GameEngine engine, GameElementTmx[] gameElementTmx)
	{
		this.engine = engine;
		
		tiledMap = new TMXTiledMap[gameElementTmx.length];
		
		for (int i = 0; i < gameElementTmx.length; i++)
		{
			try
			{
				tiledMap[i] =
					new TMXLoader(engine.getAssets(), engine.getTextureManager(), engine.getVertexBufferObjectManager()).loadFromAsset(gameElementTmx[i].path);
			}
			catch (TMXLoadException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	public TMXTiledMap newTiledMap(int index)
	{
		return newTiledMap(index, engine.getEngine().getScene());
	}
	
	public TMXTiledMap newTiledMap(int index, Scene scene)
	{
		Utils.TRACE("SIZE = " + tiledMap[index].getTMXLayers().size());
		
		tmxLayer = new TMXLayer[tiledMap.length][tiledMap[index].getTMXLayers().size()];
		for (int i = 0; i < tiledMap[index].getTMXLayers().size(); i++)
		{
			tmxLayer[index][i] = tiledMap[index].getTMXLayers().get(i);
			scene.attachChild(tiledMap[index].getTMXLayers().get(i));
		}
		
		return tiledMap[index];
	}
}
